<?php

/**
 * This is the model class for table "cabs_game".
 *
 * The followings are the available columns in table 'cabs_game':
 * @property integer $id
 * @property string $name
 * @property string $slug
 * @property integer $bggId
 * @property integer $minPlayers
 * @property integer $maxPlayers
 * @property integer $playTime
 * @property string $weight
 * @property string $average
 * @property integer $minAge
 * @property string $create_time
 * @property integer $create_user_id
 * @property string $update_time
 * @property integer $update_user_id
 *
 * The followings are the available model relations:
 * @property Library[] $libraries
 */
class Game extends CabsActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Game the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cabs_game';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('bggId, minPlayers, maxPlayers, playTime, minAge', 'numerical', 'integerOnly'=>true),
			array('name, slug', 'length', 'max'=>255),
			array('weight', 'length', 'max'=>6),
			array('average', 'length', 'max'=>7),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, slug, bggId, minPlayers, maxPlayers, playTime, weight, average, minAge, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'libraries' => array(self::HAS_MANY, 'Library', 'gameId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'slug' => 'Slug',
			'bggId' => 'Bgg',
			'minPlayers' => 'Min Players',
			'maxPlayers' => 'Max Players',
			'playTime' => 'Play Time',
			'weight' => 'Weight',
			'average' => 'Average',
			'minAge' => 'Min Age',
			'create_time' => 'Created On',
			'create_user_id' => 'Created By',
			'update_time' => 'Last Updated On',
			'update_user_id' => 'Last Updated By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('slug',$this->slug,true);
		$criteria->compare('bggId',$this->bggId);
		$criteria->compare('minPlayers',$this->minPlayers);
		$criteria->compare('maxPlayers',$this->maxPlayers);
		$criteria->compare('playTime',$this->playTime);
		$criteria->compare('weight',$this->weight,true);
		$criteria->compare('average',$this->average,true);
		$criteria->compare('minAge',$this->minAge);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('create_user_id',$this->create_user_id);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user_id',$this->update_user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	 // Behaviors for this model
	public function behaviors(){
		return array(
			'sluggable' => array(
				'class'=>'ext.behaviors.SluggableBehavior.SluggableBehavior',
				'columns' => array('name'),
				'unique' => true,
				'update' => false,
			),
		);
	}
	
	// to get base game
	public function getGameDisplay()
	{
		return $this->name;
	}
}